package com.ruoyi.lowcode.utils;

import com.ruoyi.lowcode.domain.vo.ExportModelVo;
import com.ruoyi.lowcode.domain.vo.LcFieldVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class POIUtils {

    /** sheet最大数据行数 */
    private static final int sheetCountNum = 10000;

    /**
     * 生成excel并下载
     *
     * @param exportModel
     * @param response
     */
    public static void exportExcel(ExportModelVo exportModel, HttpServletResponse response) throws IOException {
        // 创建工作簿类
        XSSFWorkbook workbook = new XSSFWorkbook();
        // excel表sheet个数
        int sheetNum = exportModel.getDataList().size()/sheetCountNum + 1;

        for (int i = 0; i < sheetNum; i++){
            // 创建工作表并设置表名
            XSSFSheet sheet = workbook.createSheet(exportModel.getModel().getModelName());
            // 设置标题标题
            XSSFRow modelTitle = sheet.createRow(0);
            XSSFCell cell = modelTitle.createCell(0);
            cell.setCellValue(exportModel.getModel().getModelName());

            // 设置字段标题
            XSSFRow fieldTitle = sheet.createRow(1);
            int fieldIndex = 0;
            for (LcFieldVo field : exportModel.getFieldList()){
                fieldTitle.createCell(fieldIndex).setCellValue(field.getRemark());
                fieldIndex++;
            }

            List<Map<String, Object>> valueList = new ArrayList<>();

            if(((i + 1) * sheetCountNum) > exportModel.getDataList().size()){
                valueList = exportModel.getDataList().subList(i * sheetCountNum, exportModel.getDataList().size());
            }else {
                valueList = exportModel.getDataList().subList(i * sheetCountNum, ((i + 1) * sheetCountNum));
            }

            int rowIndex = 2;

            for (Map<String, Object> rowInfo : valueList){
                XSSFRow rowValue = sheet.createRow(rowIndex);
                int valueIndex = 0;
                for (Object value : rowInfo.values()){
                    if(ObjectUtils.isEmpty(value)){
                        rowValue.createCell(valueIndex).setCellValue("");
                    }else {
                        rowValue.createCell(valueIndex).setCellValue(value.toString());
                    }
                    valueIndex++;
                }
                rowIndex++;
            }

            // 标题单元格合并
            CellRangeAddress region = new CellRangeAddress(0, 0, 0, exportModel.getFieldList().size() - 1);
            sheet.addMergedRegion(region);
            // 居中
            XSSFCellStyle borderStyle = workbook.createCellStyle();
            borderStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            borderStyle.setAlignment(HorizontalAlignment.CENTER);
            cell.setCellStyle(borderStyle);
        }

        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=");
        workbook.write(response.getOutputStream());
    }

}
